<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	 .errorText
	 {
	     color:red;
	 }
	</style>
</head>
<body>
	<form id="form1" method="post">
        <div style="padding-left:30px;padding-bottom:10px;">
            <table style="table-layout:fixed;" cellspacing="0" cellpadding="5">
            	<tr>
            		<td style="width:100px;">用户名：</td>
                    <td style="width:100px;">    
                        <input name="name" emptyText="请输入用户名"  errorMode="none"  onvalidation="checkValidation" requiredErrorText="用户名不能为空"  class="mini-textbox" required="true" vtype="minLength:2;maxLength:30" style="width: 130px" />
                    </td>
                 	<td id="name_error" class="errorText" ></td>
            	</tr>
            	<tr>
            		<td style="width:20px;">登录名：</td>
                    <td style="width:100px;">    
                        <input name="username" onblur="isRepeated();" errorMode="none" onvalidation="checkValidation" requiredErrorText="登录名不能为空"  class="mini-textbox" required="true" vtype="minLength:4;maxLength:20" style="width: 130px" emptyText="请确保登录名的唯一性"/>
                    </td>
                    <td id="username_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:20px;">密码：</td>
                    <td style="width:100px;">    
                        <input name="password" errorMode="none" onvalidation="checkValidation" requiredErrorText="密码不能为空"  class="mini-password" required="true" vtype="minLength:5;maxLength:15" style="width: 130px"/>
                    </td>
                    <td id="password_error" class="errorText" ></td>
                </tr>
                <tr>
               		<td style="width:20px;">部门：</td>
                    <td>
						<input id="combo1" name="deptType" class="mini-combobox" style="width:150px;" textField="keyValue" valueField="keyName" emptyText="请选择..."
				        		url="dictionary_key_val_view.do?typeName=DEPT_TYPE" onValueChanged="changeValue" required="true" allowInput="false" showNullItem="true" nullItemText="请选择..."/> 
                    </td>  
                </tr>
                <tr id="blongs_group" style="display:none;">
                	<td style="width:20px;">所属组：</td>
                	<td>
						<input id="combo2" name="groupId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
    						url="user_group_view.do" popupWidth="150px;" popupHeight="120px;" allowInput="false" showNullItem="true" required="true" nullItemText="请选择..."/>
                    </td>
                </tr>
                <tr id="leader_group" style="display:none;">
                	<td style="width:20px;">是否组长：</td>
                	<td>
<!-- 						<input id="is_leader" class="mini-radiobuttonlist" name="isLeader" required="true" data=[{id:"1",text:"组长"},{id:"2",text:"组员"}] /> -->
						<input id="is_leader" class="mini-radiobuttonlist" name="isLeader" data=[{id:"1",text:"组长"},{id:"2",text:"组员"}] />
                    </td>
                </tr>
                <tr>
                	<td style="width:20px;">角色：</td>
                    <td>
							<div id="combo3" emptyText="请选择..."  class="mini-combobox" style="width:150px;" name="roleId" popupWidth="165" textField="name" valueField="id" 
							    url="user_role_view.do" multiSelect="true" required="true" showClose="true" oncloseclick="onCloseClick" >     
							    <div property="columns">
							        <div header="角色(精确)" field="name"></div>
							    </div>
							</div>
                    </td>
                </tr>
                <tr>
                    <td style="width:20px;">联系电话：</td>
                    <td style="width:100px;">    
                        <input name="mobile" emptyText="请输入电话号码"  errorMode="none" onvalidation="checkValidation"  class="mini-textbox" vtype="maxLength:15" style="width: 130px"/>
                    </td>
                    <td id="mobile_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:20px;">备注：</td>
                    <td colspan="3">    
                        <input name="description" emptyText="请输入备注"  class="mini-textarea" vtype="maxLength:255" style="width:200px;" />
                    </td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding:10px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>
</body>
</html>

<script type="text/javascript">
	mini.parse();
	
	var form = new mini.Form("form1");
	
	//保存数据
	function SaveData() {
	    var o = form.getData(true);
	    form.validate();
	    if (form.isValid() == false || isRepeated() == false) return;
    	var username = mini.getbyName('username').getValue();
		var dept = mini.get("combo1").getValue();
		if(dept == 'cm' || dept == 'om') {
   				if(!/^\d{4}$/.test(username)) {
	   				mini.alert('话务中心、核单中心用户的登录名为长度4位的工号(数字)');
	   				return;
   				} else {
   					save(o);
   				}
		} else {
			save(o);
		}
	}
	
	function save(o) {
		$.ajax({
	        url: "medical_user_save.do",
			type: 'post',
			dataType: 'json',
	        data: o,
	        cache: false,
	        success: function (text) {
	            if(text.success){
	            	window.CloseOwnerWindow("succ");
	            }else{
	            	mini.alert("数据错误！");
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
	}
	//判断登录名称是否重复
    function isRepeated(){
    	var username = mini.getbyName('username').getValue();
    	var flag = true;
    	if(username.length > 0){
	  	    $.ajax({
	  	    	url: "verify_username_only.do?username=" + username,
	  	    	dataType: 'json',
	  	        cache: false,
		    	async: false,
	 	        success: function (text) {
	 	            if(text.result == "true"){
	 	            	flag = true;
	 	            }else if(text.result == "false"){
	 	            	mini.alert("登录名称重复请修改");
	 	            	flag = false;
	 	            }else{
	 	            	mini.alert("数据错误");
	 	            	flag = false;
	 	            }
	 	        }
	  	    });
    	}
  	    return flag;
    }

    function GetData() {
        var o = form.getData();
        return o;
    }
    
    function SetData(data) {
		data = mini.clone(data);
 		form.setData(data);
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
    
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }
    
    function updateError(e) {
        var id =e.sender.name + "_error";
        var el = document.getElementById(id);
        if (el) {
            el.innerHTML =e.errorText;
        }
    }
    
    function checkValidation(e) {                  
        updateError(e);
    }
    
    function changeValue() {
   		var dept = mini.get("combo1").getValue();
   		if(dept == 'cm' || dept == 'om') {
   			$("#blongs_group").show();
   			$("#leader_group").show();
   		} else {
   			$("#blongs_group").hide();
   			$("#leader_group").hide();
   		}
   		mini.get("is_leader").setValue(null);
		mini.get("combo2").setValue(null);
   	}
    
    function onCloseClick(e) {
        var obj = e.sender;
        obj.setText("");
        obj.setValue("");
    }
</script>